package work6;

public class Sum implements Runnable {
    private int start;
    private int end;
    private static int totalSum = 0; 

    public Sum(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        int sum = 0;
        for (int i = start; i <= end; i++) {
            sum += i;
        }
        
        synchronized (Sum.class) {
            totalSum += sum;
        }
    }

    public static int getTotalSum() {
        return totalSum;
    }

    public static void main(String[] args) {
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            int start = i * 10 + 1;
            int end = (i + 1) * 10;
            threads[i] = new Thread(new Sum(start, end));
            threads[i].start();
        }

        for (Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("Total Sum: " + getTotalSum());
    }
}
